package com.inctech.controller.employee;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.inctech.model.Organization;
import com.inctech.service.IOrganizationService;

@Controller
@RequestMapping("organization")
public class OrganizationController {

	final Logger logger = LoggerFactory.getLogger(OrganizationController.class);
	
	@Resource(name = "organizationService")
	private IOrganizationService organizationService;
	
	//organization.jsp
	@RequestMapping("/organization")
	public String organization() {
		return "employee/organization";
	}
	
	@ResponseBody
	@RequestMapping("/getOrganizationTreeData")
	public String getOrganizationTreeData() {
		JSONObject childObj = new JSONObject();
		JSONArray childArr = new JSONArray();
		JSONObject parentObj = new JSONObject();
		JSONArray parentArr = new JSONArray();
		//查找所有的部门
		List<String> department = organizationService.getAllDepartment();
		//查找所有的组织机构
		List<Organization> organization = organizationService.getAllOrganization();
		
		for(int i=0,size1=department.size();i<size1;i++){
			parentObj.put("id", i+1);
			parentObj.put("text", department.get(i));
			parentObj.put("state", "closed");
			String depar = department.get(i);
			int k = 1;
			for(int j=0,size2=organization.size();j<size2;j++){
				if(organization.get(j).getDepartment().equals(depar)){
					childObj.put("id", (i+1)*100+k);
					childObj.put("text", organization.get(j).getPosition());
					childArr.add(childObj);
					childObj.clear();
					k++;
				}
			}
			parentObj.put("children", childArr);
			childArr.clear();
			parentArr.add(parentObj);
			parentObj.clear();
		}
		
		return parentArr.toString();
	}
	
	@ResponseBody
	@RequestMapping("/addDepartment")
	public String addDepartment(Organization org) {
		JSONObject Obj = new JSONObject();
		int count = 0;
		count = organizationService.addDepartment(org);
		if(count==1){
			Obj.put("result", true);
		}else{
			Obj.put("result", false);
		}
		return Obj.toString();
	}
	
	@ResponseBody
	@RequestMapping("/updateDepartment")
	public String updateDepartment(String pre_department,String now_department) {
		JSONObject Obj = new JSONObject();
		int count = 0;
		count = organizationService.updateDepartment(pre_department, now_department);
		if(count>=1){
			Obj.put("result", true);
		}else{
			Obj.put("result", false);
		}
		return Obj.toString();
	}
	
	@ResponseBody
	@RequestMapping("/addPosition")
	public String addPosition(Organization org) {
		JSONObject Obj = new JSONObject();
		int count = 0;
		count = organizationService.addPosition(org);
		if(count==1){
			Obj.put("result", true);
		}else{
			Obj.put("result", false);
		}
		return Obj.toString();
	}
	
	@ResponseBody
	@RequestMapping("/updatePosition")
	public String updatePosition(String department,String pre_position,String now_position) {
		JSONObject Obj = new JSONObject();
		int count = 0;
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("department", department);
		paramMap.put("pre_position", pre_position);
		paramMap.put("now_position", now_position);
		count = organizationService.updatePosition(paramMap);
		if(count==1){
			Obj.put("result", true);
		}else{
			Obj.put("result", false);
		}
		return Obj.toString();
	}
	
	
}
